﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.purchaseTableAdapters;
using dhDataAcess.dataAcess.ViewDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;
using dhDataAcess.produceLinq;
using dhDataAcess.produceDAL;
using dhDataAcess.sysDAL;

namespace dhSystemBLL.produceBLL
{
   public  class pdckBLL
    {
        private V_pdckTableAdapter  _pdckAdapter = null;
        private V_pdckTableAdapter pdckAdapter
        {
            get
            {
                if (_pdckAdapter == null) _pdckAdapter = new V_pdckTableAdapter();
                return _pdckAdapter;

            }
        }
       private pdckMasterTableAdapter _pdckMasterlAdapter = null;
       private pdckMasterTableAdapter pdckMasterAdapter
        {
            get
            {
                if (_pdckMasterlAdapter == null) _pdckMasterlAdapter = new pdckMasterTableAdapter();
                return _pdckMasterlAdapter;

            }
        }
       private pdckDetailTableAdapter _pdckDetailAdapter = null;
       private pdckDetailTableAdapter pdckDetailAdapter
        {
            get
            {
                if (_pdckDetailAdapter == null) _pdckDetailAdapter = new pdckDetailTableAdapter();
                return _pdckDetailAdapter;

            }
        }
       /// <summary>
       /// 查询视图表
       /// </summary>
       /// <returns></returns>

       public ViewDataSet.V_pdckDataTable getV_pdckALL()
       {
           try
           {
               return pdckAdapter.GetDataV_pdckALL();
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
        /// <summary>
        /// 查询领料出库主表
        /// </summary>
        /// <returns></returns>

       public purchase.pdckMasterDataTable getpdckMasterAll()
        {
            try
            {
                return pdckMasterAdapter.GetDatapdckMasterAll ();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询领料出库从表
        /// </summary>
        /// <returns></returns>

       public purchase.pdckDetailDataTable getpdckDetailAll()
        {
            try
            {
                return pdckDetailAdapter.GetDatapdckDetailAll ();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据入库单号查询领料出库表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
       public purchase.pdckMasterDataTable getpdckMasterAllBybillNo(string billNo)
        {
            try
            {
                return pdckMasterAdapter.GetDatapdckMasterBybillNo (billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        /// <summary>
        /// 根据入库单号查询领料出库从表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
       public purchase.pdckDetailDataTable getspdckDetailAllBypName(string pName)
        {
            try
            {
                return pdckDetailAdapter.GetDatapdckDetailBypName (pName);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
       /// <summary>
       /// 根据入库单号查询领料出库从表
       /// </summary>
       /// <param name="groupNum"></param>
       /// <returns></returns>
       public purchase.pdckDetailDataTable getspdckDetailAllBybillNo(string billNo)
       {
           try
           {
               return pdckDetailAdapter.GetDatapdckDetailBybillNo (billNo);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
        /// <summary>
        /// 修改领料出库表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int pdckInsert(purchase.pdckMasterDataTable masterTable, purchase.pdckDetailDataTable pTable, string state, string billNo, string Date)
        {
            pdckDetailModel pdckDetailRow = new pdckDetailModel();
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                pdckMasterDAL pdckMaster = new pdckMasterDAL(tn, conn);
                //pdckDetailDAL pdckDetail = new pdckDetailDAL(tn, conn);
                pdckMaster.pdckMasterInsertII(masterTable, pTable);
   

                tn.Commit();
                return i;

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个出库单号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }


        /// 删除一行领料出库表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int pdckMasterDel(string billNo)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                pdckMasterDAL pdckMaster = new pdckMasterDAL(tn, conn);
                pdckMaster.pdckDel(billNo);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
       /// 删除一行胚带入库表
       /// </summary>
       /// <param name="row"></param>
       /// <returns></returns>
       public int pdckMasterDelII(string billNo)
       {
           int i = 0;
           SqlConnection conn = dataConn.connOpen();
           SqlTransaction tn = null;
           try
           {
               tn = conn.BeginTransaction();
               pdckMasterDAL pdckMaster = new pdckMasterDAL(tn, conn);
               pdckMaster.pdckDelII(billNo);
               //  pdrkMaster.pdrkMasterDel(billNo);
               tn.Commit();
               return i;
           }
           catch (Exception ex)
           {
               tn.Rollback();
               throw new Exception(ex.Message.ToString());

           }
           finally
           {
               conn.Close();
           }
       }
    }
}
